package com.nit.lab.controller.basic;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.nit.lab.dto.basic.StudentDTO;
import com.nit.lab.dto.system.TokenDTO;
import com.nit.lab.dto.utils.OpResultDTO;
import com.nit.lab.dto.utils.TableReqDTO;
import com.nit.lab.dto.utils.TableRspDTO;
import com.nit.lab.service.basic.StudentService;
import com.nit.lab.utils.HttpRequestReader;
import com.nit.lab.utils.JWTUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @文件名：StudentController
 * @创建人: lxr
 * @创建时间: 2020/3/23
 * @描述:
 */
@RestController
@RequestMapping(value = "/api/student")
public class StudentController {
    @Resource
    private StudentService studentService;
    private final Logger logger = LoggerFactory.getLogger(StudentController.class);
    /**
     * @Description: 获取表格数据
     * @Author: lxr
     * @Date: 2020/03/23
     */
    @RequestMapping(value = "/query", method = RequestMethod.POST)
    public TableRspDTO list4Table(@RequestHeader(name = "Access-Token") String token, @RequestBody TableReqDTO tableReqDTO) {
        TableRspDTO tableRspDTO = new TableRspDTO();
        try {
            TokenDTO tokenDTO = JWTUtil.verifyToken(token);
            tableRspDTO = studentService.list4Table(tableReqDTO);
        } catch (Exception e) {
            logger.error(e.toString());
        }
        return tableRspDTO;
    }
    /**
     * @Description: 新增学生记录
     * @Author: lxr
     * @Date: 2020/03/23
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public OpResultDTO add(@RequestHeader(name = "Access-Token") String token, @RequestBody StudentDTO studentDTO) {
        OpResultDTO opResult = new OpResultDTO();
        try {
            TokenDTO tokenDTO = JWTUtil.verifyToken(token);
            opResult.setMessage(studentService.add(studentDTO).toString());
        } catch (Exception e) {
            logger.error(e.toString());
            opResult.setMessage("error");
        }
        return opResult;
    }
    /**
     * @Description: 批量新增学生记录
     * @Author: lxr
     * @Date: 2020/03/30
     */
    @RequestMapping(value = "/addList", method = RequestMethod.POST)
    public OpResultDTO addStudents(@RequestHeader(name = "Access-Token") String token, HttpServletRequest request) {
        OpResultDTO opResult = new OpResultDTO();

        try {
            TokenDTO tokenDTO = JWTUtil.verifyToken(token);
            JSONObject jsonRequest = HttpRequestReader.getJsonObject(request);
            JSONArray studentList = jsonRequest.getJSONArray("studentList");
            List<StudentDTO> studentDTOList = JSONObject.parseArray(studentList.toJSONString(),StudentDTO.class);
            if (studentService.addStudents(studentDTOList) == studentList.size()) {
                opResult.setMessage("success");
            }
            else {
                opResult.setMessage("error");
            }
        } catch (Exception e) {
            logger.error(e.toString());
            opResult.setMessage("error");
        }
        return opResult;
    }
    /**
     * @Description: 登录账号重名验证
     * @Author: lxr
     * @Date: 2020/03/23
     */
    @RequestMapping(value = "/validateStudentCode", method = RequestMethod.POST)
    public OpResultDTO validateLoginName(HttpServletRequest request) {
        OpResultDTO opResult = new OpResultDTO();
        try {
            JSONObject jsonRequest = HttpRequestReader.getJsonObject(request);
            String studentCode = jsonRequest.getString("studentCode");
            if (studentService.loginCheck(studentCode) == null) {
                opResult.setMessage("success");
            } else {
                opResult.setMessage("error");
            }
        } catch (Exception e) {
            logger.error(e.toString());
            opResult.setMessage("error");
        }
        return opResult;
    }
    /**
     * @Description: 更新学生信息
     * @Author: lxr
     * @Date: 2020/03/23
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public OpResultDTO update(@RequestBody StudentDTO studentDTO) {
        OpResultDTO opResult = new OpResultDTO();
        try {
            opResult.setMessage(studentService.update(studentDTO).toString());
        } catch (Exception e) {
            logger.error(e.toString());
            opResult.setMessage("error");
        }
        return opResult;
    }
    /**
     * @Description: 删除学生信息
     * @Author: lxr
     * @Date: 2020/03/23
     */
    @RequestMapping(value = "/remove", method = RequestMethod.POST)
    public OpResultDTO remove(HttpServletRequest request) {
        OpResultDTO opResult = new OpResultDTO();
        try {
            JSONObject jsonRequest = HttpRequestReader.getJsonObject(request);
            String studentIds = jsonRequest.getString("studentIds");
            opResult.setMessage(studentService.remove(studentIds).toString());
        } catch (Exception e) {
            logger.error(e.toString());
            opResult.setMessage("error");
        }
        return opResult;
    }
    /**
     * @Description: 重置学生密码
     * @Author: lxr
     * @Date: 2020/03/23
     */
    @RequestMapping(value = "/resetPwd", method = RequestMethod.POST)
    public OpResultDTO resetPwd(@RequestBody StudentDTO studentDTO) {
        OpResultDTO opResult = new OpResultDTO();
        try {
            opResult.setMessage(studentService.resetPwd(studentDTO.getStudentId(), studentDTO.getStudentPwd()).toString());
        } catch (Exception e) {
            logger.error(e.toString());
            opResult.setMessage("error");
        }
        return opResult;
    }
    /**
     * @Description: 根据StudentId获取学生信息
     * @Author: lxr
     * @Date: 2020/03/25
     */
    @RequestMapping(value = "/getInfo", method = RequestMethod.POST)
    public OpResultDTO getInfo(@RequestHeader(name = "Access-Token") String token) {
        OpResultDTO opResult = new OpResultDTO();
        try {
            TokenDTO tokenDTO = JWTUtil.verifyToken(token);
            opResult.setMessage("success");
            opResult.setResult(studentService.getByStudentId(tokenDTO.getAccountId()));
        } catch (Exception e) {
            logger.error(e.toString());
            opResult.setMessage("error");
        }
        return opResult;
    }
}
